package work;
import ordersystem.Dish;
import ordersystem.Order;
import ordersystem.MainDish;
import ordersystem.Snack;
import ordersystem.Dessert;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
public class Orderwork {
    private Scanner scanner = new Scanner(System.in);
    private static ArrayList<Dish> globalMenu = new ArrayList<>();
    private int customerCount = 0;
    public void initGlobalMenu() {
        // 主食：牛肉汉堡、鸡肉汉堡、鸡肉卷
        globalMenu.add(new MainDish("牛肉汉堡", 9.9, 100));
        globalMenu.add(new MainDish("鸡肉汉堡", 9.9, 100));
        globalMenu.add(new MainDish("鸡肉卷", 7.99, 100));
        // 小吃：薯条(小份)、薯条(大份)、章鱼小丸子(5个)
        globalMenu.add(new Snack("薯条(小份)", 5.0, 100));
        globalMenu.add(new Snack("薯条(大份)", 7.0, 100));
        globalMenu.add(new Snack("章鱼小丸子(5个)", 10.0, 100));
        // 饮品甜品：冰镇可乐、香草冰淇淋、巧克力蛋糕
        globalMenu.add(new Dessert("冰镇可乐", 4.99, 100));
        globalMenu.add(new Dessert("香草冰淇淋", 3.0, 100));
        globalMenu.add(new Dessert("巧克力蛋糕", 9.9, 100));
    }
    public void showMenu() {
        System.out.println("=====超超超超级好吃快餐店====");
        System.out.println("  优惠活动:");
        System.out.println("1.任意2种单品搭配,每套立省2元!");
        System.out.println("2.满29.99元减5元, 满39.99元减8元!");
        System.out.println("当前菜单(实时库存):");
        for (int i = 0; i < globalMenu.size(); i++) {
            Dish dish = globalMenu.get(i);
            System.out.println((i + 1) + "." + dish.getName() +
                    "- " + String.format("%.2f", dish.getPrice()) + "元" +
                    "(库存:" + dish.getStock() + "份)");
        }
    }
    public boolean processSingleOrder() {
        customerCount++;
        Order order = new Order();
        System.out.println("\n===客户" + customerCount + "点餐====");
        while (true) {
            System.out.print("请输入菜品编号(输入0结束本次点餐):");
            int choice;
            try {
                choice = scanner.nextInt();
            } catch (java.util.InputMismatchException e) {
                System.out.println("× 输入错误！请输入数字编号（1-" + globalMenu.size() + "或0）");
                scanner.next();
                continue;
            }
            if (choice == 0) {
                break;
            }
            if (choice < 1 || choice > globalMenu.size()) {
                System.out.println("× 输入错误!请输入1-" + globalMenu.size() + "之间的编号");
                continue;
            }
            Dish selectedDish = globalMenu.get(choice - 1);
            System.out.print("请输入购买数量:");
            int num;
            try {
                num = scanner.nextInt();
            } catch (java.util.InputMismatchException e) {
                System.out.println("× 输入错误！请输入正整数数量");
                scanner.next();
                continue;
            }
            if (num <= 0) {
                System.out.println("× 数量不能为0或负数!");
                continue;
            }
            if (num > selectedDish.getStock()) {
                System.out.println("× 库存不足!当前仅剩余" + selectedDish.getStock() + "份");
                continue;
            }
            order.addSingleDish(selectedDish, num);
        }
        System.out.println("\n=====客户" + customerCount + "的订单详情===");
        order.calculateComboDiscount();
        order.showOrder();
        saveOrderToFile(order, customerCount);
        System.out.print("是否继续接待下一位用户?(输入1=继续,0=退出):");
        int nextChoice = scanner.nextInt();
        return nextChoice == 1;
    }
    public void saveOrderToFile(Order order, int customerId) {
        String dirPath = "D:\\点餐小票";
        File dir = new File(dirPath);
        if (!dir.exists()) {
            dir.mkdirs();
            System.out.println("已创建文件夹: " + dirPath);
        }
        String timeStr = new Date().toString().replaceAll(":", "-").replaceAll(" ", "_");
        String filePath = dirPath + "\\客户" + customerId + "_" + timeStr + ".txt";
        try (PrintWriter writer = new PrintWriter(filePath, "UTF-8")) {
            writer.println("===== 超超超超级好吃快餐店 点餐小票 =====");
            writer.println("客户编号: " + customerId);
            writer.println("下单时间: " + new Date());
            writer.println("========================================\n");
            writer.print(order.toText());
            System.out.println("\n✅ 订单已保存到: " + filePath);
        } catch (IOException e) {
            System.err.println("❌ 保存订单失败: " + e.getMessage());
        }
    }
    public void closeScanner() { scanner.close(); }
}